package com.sskj.dgzs.repository;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.sskj.core.query.annotation.DynamicQuery;
import com.sskj.core.repository.GenericRepository;
import com.sskj.dgzs.domain.DengGuangZhouShouQueryParam;
import com.sskj.dgzs.domain.entity.DengGuangZhuShouEntity;

public interface DengGuangZhuShouRepository extends GenericRepository<DengGuangZhuShouEntity, String> {

	@Query(value = " from DengGuangZhuShouEntity x where 1=1 "
			+ "<notEmpty name='creatorId'> and x.creatorId=:creatorId </notEmpty> "
			+ "<notEmpty name='text'> and (x.title like '%${text}%' or x.viewdesc like '%${text}%' or x.tjrNick like '%${text}%' ) </notEmpty> "
			+ "<notEmpty name='suggesttype'> and x.suggesttype = :suggesttype </notEmpty> order by  x.createdTime desc")
	@DynamicQuery
	Page<DengGuangZhuShouEntity> findPage(DengGuangZhouShouQueryParam queryParam, Pageable pageable);

	@Query("SELECT r FROM DengGuangZhuShouEntity r " + " where r.creatorId = ?1  order by r.createdTime desc")
	Page<DengGuangZhuShouEntity> queryLastSuggsion(String creatorId, Pageable pageable);

	@Query(value = "SELECT * FROM lsgd_deng_guang_zhu_shou_entity r "
			+ " where r.suggesttype=?1  order by r.created_time desc LIMIT 1", nativeQuery = true)
	DengGuangZhuShouEntity queryLastSuggsionByType(Integer suggestType);

	@Transactional
	@Modifying(clearAutomatically = true)
	@Query("update DengGuangZhuShouEntity x set x.datastatus=?1 where x.id = ?2")
	void fallBackSuggsion(int datastatus, String id);
}
